function wueDataFactory () {
    this.areaInfo = null;
    this.tableData = null;
    this.activeBtn = 1;
    var me = this;
    // 获取分区信息数据
    this.loadData1 = function () {
        var def = $.Deferred();
        $.getJSON("/jsite-szy-dispatch/a/szy/dispatch/ddsWeWrcs/list", function (data) {
            me.areaInfo = data;
            def.resolve();
        });
        return def.promise();
    }
    // 获取蓄水动态评价表格数据
    this.loadData2 = function () {
        var def = $.Deferred();
        $.getJSON("/jsite-szy-dispatch/a/szy/dispatch/ddsWeWue/listAllToMap", function (data) {
            me.tableData = data;
            def.resolve();
        });
        return def.promise();
    }
}

wueDataFactory.prototype = {
    // 从tableData中获取年份数据
    getYearsFromTableData: function () {
        var data = this.tableData;
        var result = [];
        for (var year in data) {
            result.push({
                'text': year,
                'value': year
            });
        }
        return result;
    },
    setTableDataByType: function () {
        var year = Ext.getCmp("wueCombo").getValue();
        var currentData = this.tableData[year];
        var type = this.activeBtn;
        var grepData = $.grep(currentData, function (obj) {
            return obj.regType == type;
        });
        Ext.data.StoreManager.lookup('wue_store1').setData(grepData);
    }
}
var wueData;


Ext.define('Admin.view.evaluation.wue.WueController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.wueController',
    views: ['Admin.view.evaluation.wue.Wue'],
    initData: function (_this, opt) {
        var myMask = new Ext.LoadMask(Ext.ComponentQuery.query('wue')[0], { msg: "正在加载..." });
        myMask.show();
        var me = this;
        wueData = new wueDataFactory();
        $.when(wueData.loadData1(), wueData.loadData2())
            .done(function () {
                // 设置年份数据
                var yearData = wueData.getYearsFromTableData();
                var firstSelect = yearData.length > 0 ? yearData[0].value : '';
                var year_store = Ext.create('Ext.data.Store', {
                    fields: ['text', 'value'],
                    data: yearData
                });
                Ext.create('Ext.form.ComboBox', {
                    renderTo: 'wueComboWrapper',
                    id: 'wueCombo',
                    labelWidth: 35,
                    emptyText: '请选择',
                    fieldLabel: "年份",
                    valueField: 'value',
                    displayField: 'text',
                    value: firstSelect,
                    editable: false,
                    store: year_store,
                    listeners: {
                        'change': me.changeYear
                    }
                });
                Ext.getCmp('wueBtn1').fireEvent('click');
                myMask.hide();
            });
    },
    changeYear: function (_this, newVal, oldVal, opt) {
        wueData.setTableDataByType();
    },
    switchAdminRegion: function () {
        const type = 0;
        wueData.activeBtn = type;
        wueData.setTableDataByType();
    },
    switchWaterRegion: function () {
        const type = 1;
        wueData.activeBtn = type;
        wueData.setTableDataByType();
    },

})

